package yxy.game.pm2.module.arena.toparena;

import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.BaseGameRedisBean;

/**
 * @ClassName TopAreanPlayerModule
 * @Author jingtao-zheng
 * @Date 2022/9/17 17:57
 * @Version 1.0
 **/
public class TopAreanPlayerModule extends BaseGameRedisBean {
    private String code;// 赛季code
    private String playerCode;
    private int pos;// 玩家位置
    private int group;// 玩家分组
    private TopAreanHandle.TopAreanLevel nowLevel = TopAreanHandle.TopAreanLevel.None;//当前成绩 默认未参赛
    private TopAreanHandle.TopAreanLevel bestLevel = TopAreanHandle.TopAreanLevel.None;//最好成绩 默认未参赛

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getPlayerCode() {
        return playerCode;
    }

    public void setPlayerCode(String playerCode) {
        this.playerCode = playerCode;
    }

    public int getPos() {
        return pos;
    }

    public void setPos(int pos) {
        this.pos = pos;
    }

    public int getGroup() {
        return group;
    }

    public void setGroup(int group) {
        this.group = group;
    }

    public TopAreanHandle.TopAreanLevel getNowLevel() {
        return nowLevel;
    }

    public void setNowLevel(TopAreanHandle.TopAreanLevel nowLevel) {
        this.nowLevel = nowLevel;
    }

    public TopAreanHandle.TopAreanLevel getBestLevel() {
        return bestLevel;
    }

    public void setBestLevel(TopAreanHandle.TopAreanLevel bestLevel) {
        this.bestLevel = bestLevel;
    }

    //====================================== func
    //更新当前成绩和最好成绩
    public void rankLevelUp(TopAreanHandle.TopAreanLevel level) {
        setNowLevel(level);//设置当前等级
        if (level.getNumber() > 0 && level.getNumber() < getBestLevel().getNumber()) {
            setBestLevel(level);
        }
    }




    // ====================================== redis
    static public TopAreanPlayerModule get(String playerCode) {
        try {
            TopAreanPlayerModule module = getv(key(playerCode));
            if (module == null) {
                module = MySQLCacheDAO.getV(key(playerCode), TopAreanPlayerModule.class);
                if(module != null){module.set();}//把数据同步到redis
            }
            if (module == null) {
                module = create(playerCode);
            }
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    static private TopAreanPlayerModule create(String playerCode) {
        TopAreanPlayerModule module = new TopAreanPlayerModule();
        module.setPlayerCode(playerCode);
        module.setCode("xx");
        module.set();
        return module;
    }

    public void set() {
        try {
            setv(key(getPlayerCode()), this);
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                TopAreanPlayerModule topAreanPlayerModule = getv(key(playerCode));
                MySQLCacheDAO.setV(key, topAreanPlayerModule);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    static public String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:TOPAREAN", playerCode);
    }

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }
}
